//
//  SelectTagView.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/21.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "SelectTagView.h"
#import "SelectTypeCell.h"
@interface SelectTagView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *bgBlackView;
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UIView *selectView;
@property (nonatomic, assign) NSInteger counter;
@property (nonatomic, strong) NSMutableArray *selectArray;

@end

static NSString *tagCell = @"tagCell";
@implementation SelectTagView

- (instancetype)initWithDataSource:(NSMutableArray *)dataArray{
    if (self = [super init]) {
        self.dataArray = dataArray;
        [self loadSubviews];
    }
    return self;
}


- (void)loadSubviews{
    // window
    self.backgroundColor = [UIColor clearColor];
    self.window = [UIApplication sharedApplication].keyWindow;
    [self.window addSubview:self.bgBlackView];
    UIView *selectView = [[UIView alloc]init];
    self.selectView = selectView;
    selectView.backgroundColor = [UIColor whiteColor];
    selectView.layer.masksToBounds = YES;
    selectView.layer.cornerRadius = 8*WIDTH;
    [self.window addSubview:selectView];
    [selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.window);
        make.top.equalTo(self.window).offset(164*HEIGHT);
        make.width.mas_equalTo(320*WIDTH);
        make.height.mas_equalTo(340*HEIGHT);
    }];
    // 提示
    UIView *topTintView = [[UIView alloc]init];
    topTintView.backgroundColor = [UIColor whiteColor];
    [selectView addSubview:topTintView];
    [topTintView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(selectView);
        make.height.mas_equalTo(61*HEIGHT);
    }];
    
    
    UILabel *tintLb = [[UILabel alloc]init];
    tintLb.text = @"请选择标签（最多四个）";
    tintLb.textColor = KLColor_B12;
    tintLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15*WIDTH];
    [topTintView addSubview:tintLb];
    [tintLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topTintView);
        make.top.equalTo(topTintView).offset(20*HEIGHT);
//        make.left.equalTo(topTintView).offset(15*WIDTH);
    }];
    
    
    UIView *topLineView = [[UIView alloc]init];
    topLineView.backgroundColor = KLColor_B3;
    [topTintView addSubview:topLineView];
    [topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(topTintView);
        make.width.mas_equalTo(270*WIDTH);
        make.height.mas_equalTo(1);
        
    }];
    
    // 下面的标签内容
    [self.selectView addSubview:self.collectionView];
    // 下面选择的按钮
    UIView *bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [selectView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(selectView);
        make.height.mas_equalTo(50*HEIGHT);
    }];
    
    UIView *bottomLineView = [[UIView alloc]init];
    bottomLineView.backgroundColor = KLColor_B3;
    [bottomView addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.centerX.equalTo(bottomView);
        make.width.mas_equalTo(270*WIDTH);
        make.height.mas_equalTo(1);
        
    }];
    
    UIView *verticalLine = [[UIView alloc]init];
    verticalLine.backgroundColor = KLColor_B3;
    [bottomView addSubview:verticalLine];
    [verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.equalTo(bottomView);
        make.height.mas_equalTo(30*WIDTH);
        make.width.mas_equalTo(1);
        
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:KLColor_B12 forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    [bottomView addSubview:cancelBtn];
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self cancel];
        self.counter = 0;
        NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"tempTag"];
        if (tempArray.count > 0) {
            if (self.tagCancelDelegate) {
                [self.tagCancelDelegate sendNext:tempArray];
            }
        }
        
    }];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(bottomView);
        make.top.equalTo(bottomLineView.mas_bottom);
        make.right.equalTo(verticalLine.mas_left);
    }];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:KLColor_A1 forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    [bottomView addSubview:confirmBtn];
    [[confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        self.counter = 0;
        [[NSUserDefaults standardUserDefaults] setObject:self.selectArray forKey:@"tempTag"];
        if (self.tagDelegate) {
            [self.tagDelegate sendNext:self.selectArray];
        }
        [self cancel];
    }];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(bottomView);
        make.top.equalTo(bottomLineView.mas_bottom);
        make.left.equalTo(verticalLine.mas_right);
    }];
    
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置collectionView的滚动方向，需要注意的是如果使用了collectionview的headerview或者footerview的话， 如果设置了水平滚动方向的话，那么就只有宽度起作用了了
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        [layout setMinimumLineSpacing:12*HEIGHT];
        [layout setMinimumInteritemSpacing:12*WIDTH];
//        [layout setItemSize:CGSizeMake(76*WIDTH, 32*HEIGHT)];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15*WIDTH, 82*HEIGHT, 270*WIDTH,156*HEIGHT) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[SelectTypeCell class] forCellWithReuseIdentifier:tagCell];
        //注册头视图
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = YES;
    }
    return _collectionView;
}

#pragma mark -- UICollectionViewDataSource
/** 每组cell的个数*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

/** cell的内容*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SelectTypeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:tagCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    [cell.titleBtn setTitle:self.dataArray[indexPath.row] forState:UIControlStateNormal];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title =  self.dataArray[indexPath.row];
    if (title.length == 2) {
        return CGSizeMake(47*WIDTH, 30*HEIGHT);
    }
    if (title.length == 3) {
        return CGSizeMake(60*WIDTH, 30*HEIGHT);
    }
    return CGSizeMake(74*WIDTH, 30*HEIGHT);
}


#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了第 %zd组 第%zd个",indexPath.section, indexPath.row);
    self.counter ++;
    if (self.counter > 4) {
        [SVProgressHUD showAlert:@"不能超过四个"];
    }else{
        SelectTypeCell *cell = (SelectTypeCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.titleBtn.backgroundColor = KLColor_A1;
        [cell.titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.selectArray addObject:self.dataArray[indexPath.row]];
    }

    
    
}


- (void)cancel{
    [self.bgBlackView removeFromSuperview];
    [self.selectView removeFromSuperview];
    [self removeFromSuperview];
    
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (UIView *)bgBlackView{
    if (!_bgBlackView) {
        _bgBlackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, KLScreenH)];
        _bgBlackView.backgroundColor = KLColor(0, 0, 0, 0.5);
    }
    return _bgBlackView;
}


- (NSMutableArray *)selectArray{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}


@end
